package com.hhs.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhs.storage.bean.*;
import com.hhs.storage.dao.XmPptDao;
import com.hhs.storage.service.BaseUserService;
import com.hhs.storage.service.XmPptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhs.storage.service.XmUserClazzService;
import com.hhs.storage.service.XmUserWorkService;
import com.hhs.storage.util.SysUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chx
 * @since 2024-10-14
 */
@Service
public class XmPptServiceImpl extends ServiceImpl<XmPptDao, XmPpt> implements XmPptService {
    @Resource
    private HttpServletRequest request;
    @Resource
    private BaseUserService baseUserService;
    @Resource
    private XmUserClazzService xmUserClazzService;
    @Resource
    private XmUserWorkService xmUserWorkService;

    @Override
    public Page<XmPpt> pageOther(InputParam inputParam) {
        Page<XmPpt> page = new Page<>();
        String loginUserId = SysUtil.getLoginUserId(request);
        BaseUser byId = baseUserService.getById(loginUserId);
        //登录者id
        inputParam.setUserId(loginUserId);
        //登录者学校id
        inputParam.setSchoolId(byId.getSchoolId());
        //登录者所在班级ids
        QueryWrapper qwc=new QueryWrapper();
        qwc.eq("user_id", loginUserId);
        List<XmUserClazz> classList = xmUserClazzService.list(qwc);
        List<String> cids = classList.stream().map(XmUserClazz::getClazzId).collect(Collectors.toList());
        inputParam.setLoginClazzIds(cids);
        //登录者所在的教研室
        QueryWrapper qww=new QueryWrapper();
        qww.eq("user_id", loginUserId);
        List<XmUserWork> workList = xmUserWorkService.list(qww);
        List<String> wids = workList.stream().map(XmUserWork::getWorkId).collect(Collectors.toList());
        inputParam.setLoginWorkIds(wids);
        
        List<XmPpt> list = this.baseMapper.pageOther(inputParam);
        for (XmPpt xpt:list){
            BaseUser bu1 = baseUserService.getById(xpt.getUserId());
            xpt.setUserAccount(bu1.getAccount());
        }
        Long count = this.baseMapper.pageOtherCount(inputParam);
        page.setTotal(count);
        page.setRecords(list);
        return page;
    }
}
